/*
    Copyright (C) 2009, Cam-Tu Nguyen, Xuan-Hieu Phan
    
    Email:	ncamtu@gmail.com; pxhieu@gmail.com
    URL:	http://www.hori.ecei.tohoku.ac.jp/~hieuxuan
    
    Graduate School of Information Sciences,
    Tohoku University
*/

package jvntagger;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

//import vn.hou.fit.vnla.bean.Tuloai;
//import vn.hou.fit.vnla.delegate.TuloaiDelegate;
//import vn.hou.fit.vnla.util.StringUtil;

import jmaxent.Classification;
import jvntagger.data.DataReader;
import jvntagger.data.DataWriter;
import jvntagger.data.Sentence;
import jvntagger.data.TaggingData;

public class MaxentTagger implements POSTagger {
	DataReader reader = new POSDataReader();
	DataWriter writer = new POSDataWriter();
	TaggingData dataTagger = new TaggingData();
	
	Classification classifier = null;
	
	public MaxentTagger(String modelDir){
		init(modelDir);
	}
	public void init(String modeldir) {
		// TODO Auto-generated method stub
		dataTagger.addContextGenerator(new POSContextGenerator(modeldir + File.separator + "featuretemplate.xml"));
		classifier = new Classification(modeldir);	
	}
	
	public HashMap<String, String> tagging_tomap(String instr) {
		List<Sentence> data = reader.readString(instr);
		HashMap<String, String> hmTag = new HashMap<String, String>();
		for (int i = 0; i < data.size(); ++i){
    		Sentence sent = data.get(i);
    		for (int j = 0; j < sent.size(); ++j){
    			String [] cps = dataTagger.getContext(sent, j);
    			String label = classifier.classify(cps);
    			
    			if (label.equalsIgnoreCase("Mrk"))
    				label = sent.getWordAt(j);
    			sent.getTWordAt(j).setTag(label);
    		}
    		
    		for (int j = 0; j < sent.size(); ++j){
    			hmTag.put(sent.getWordAt(j), sent.getTagAt(j));
			}
    		
    	}
		return hmTag;
	}

	public String tagging(String instr, Integer iLoai, Integer iID) {
		String strO = "";
		// TODO Auto-generated method stub
		System.out.println("tagging ....");
		List<Sentence> data = reader.readString(instr);
		String str = new String();
		Vector<String> vtStr = new Vector<String>();
		HashMap<String, Integer> arrHm = new HashMap<String, Integer>();
		HashMap<String, String> hmPT = new HashMap<String, String>();
		Vector<String> vtTag = new Vector<String>();
		for (int i = 0; i < data.size(); ++i){
        	
    		Sentence sent = data.get(i);
    		for (int j = 0; j < sent.size(); ++j){
    			String [] cps = dataTagger.getContext(sent, j);
    			String label = classifier.classify(cps);
    			
    			if (label.equalsIgnoreCase("Mrk"))
    				label = sent.getWordAt(j);
    			sent.getTWordAt(j).setTag(label);
    			
    			
    			//System.out.println("Dat " + StringUtil.toUTF8(sent.getTWordAt(j).getWord()) + " cÃ³ tag lÃ : " + label);
    		}
    		
    		for (int j = 0; j < sent.size(); ++j){
    			if(arrHm.containsKey(sent.getWordAt(j)) == false)
    			{
    				arrHm.put(sent.getWordAt(j), 1);
    				hmPT.put(sent.getWordAt(j), sent.getTagAt(j));
    				vtTag.add(sent.getTagAt(j));
    			}
    			else
    			{
    				arrHm.put(sent.getWordAt(j), arrHm.get(sent.getWordAt(j)) + 1);
    			}
    			//str += sent.getWordAt(j) + " ";
    			//vtStr.add(sent.getWordAt(j));
    			//strO += "Dat " + sent.getWordAt(j) + " tag: " + sent.getTagAt(j) + "<br/>";
			}
    		
    	}
		strO = "";
		//Kiá»ƒm tra Ä‘á»ƒ biáº¿t thÃªm nhÃ£n cho Ä‘iá»�u hay khoáº£n
		if(iLoai == 1) //Ä�iá»�u
		{
			for(String key : arrHm.keySet())
			{
				if(hmPT.get(key).trim().equals("N") || 
						hmPT.get(key).trim().equals("V") || 
						hmPT.get(key).trim().equals("A") || 
						hmPT.get(key).trim().equals("P") ||
						hmPT.get(key).trim().equals("Np") ||
						hmPT.get(key).trim().equals("E")
				)
				{
					Tuloai oTuloai = new Tuloai();
					oTuloai.setsTu(key.trim());
					oTuloai.setsNhan(hmPT.get(key).trim());
					oTuloai.setiSoluong(arrHm.get(key));
					oTuloai.setFK_iDieuID(iID);
					oTuloai.setFK_iKhoanID(0);
					oTuloai.setFK_iDiemID(0);
					try {
						TuloaiDelegate.Insert(oTuloai);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
//				strO += "</br>+) " + key + ": " + arrHm.get(key) + " - " + hmPT.get(key);
			}
		}
		else
			if(iLoai == 2) //Khoáº£n
			{
				for(String key : arrHm.keySet())
				{
					if(hmPT.get(key).trim().equals("N") || 
							hmPT.get(key).trim().equals("V") || 
							hmPT.get(key).trim().equals("A") || 
							hmPT.get(key).trim().equals("P") ||
							hmPT.get(key).trim().equals("Np") ||
							hmPT.get(key).trim().equals("E")
					)
					{
						Tuloai oTuloai = new Tuloai();
						oTuloai.setsTu(key);
						oTuloai.setsNhan(hmPT.get(key));
						oTuloai.setiSoluong(arrHm.get(key));
						oTuloai.setFK_iDieuID(0);
						oTuloai.setFK_iKhoanID(iID);
						oTuloai.setFK_iDiemID(0);
						try {
							TuloaiDelegate.Insert(oTuloai);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
//					strO += "</br>+) " + key + ": " + arrHm.get(key) + " - " + hmPT.get(key);
				}
			}
			else
				if(iLoai == 3) //Ä�iá»ƒm
				{
					for(String key : arrHm.keySet())
					{
						if(hmPT.get(key).trim().equals("N") || 
								hmPT.get(key).trim().equals("V") || 
								hmPT.get(key).trim().equals("A") || 
								hmPT.get(key).trim().equals("P") ||
								hmPT.get(key).trim().equals("Np") ||
								hmPT.get(key).trim().equals("E")
						)
						{
							Tuloai oTuloai = new Tuloai();
							oTuloai.setsTu(key);
							oTuloai.setsNhan(hmPT.get(key));
							oTuloai.setiSoluong(arrHm.get(key));
							oTuloai.setFK_iDieuID(0);
							oTuloai.setFK_iKhoanID(0);
							oTuloai.setFK_iDiemID(iID);
							try {
								TuloaiDelegate.Insert(oTuloai);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
//						strO += "</br>+) " + key + ": " + arrHm.get(key) + " - " + hmPT.get(key);
					}
				}
		return writer.writeString(data) + "</br>";
	}

	
	public String tagging(File file) {
		// TODO Auto-generated method stub
		List<Sentence> data = reader.readFile(file.getPath());
		for (int i = 0; i < data.size(); ++i){
        	
    		Sentence sent = data.get(i);
    		for (int j = 0; j < sent.size(); ++j){
    			String [] cps = dataTagger.getContext(sent, j);
    			String label = classifier.classify(cps);
    			
    			if (label.equalsIgnoreCase("Mrk"))
    				label = sent.getWordAt(j);
    			
    			sent.getTWordAt(j).setTag(label);    
    			//System.out.println(sent.getTagAt(j));
    		}
    	}
		
		return writer.writeString(data);
	}

	public void setDataReader(DataReader reader){
		this.reader = reader;
	}
	
	public void setDataWriter(DataWriter writer){
		this.writer = writer;
	}
}
